const market = 'cryptopia'
const data = {
	Contract: '',
	AskPrice: 0,
	AskVolume: 0,
	BidPrice: 0,
	BidVolume: 0,
	LastPrice: 0,
	Time: Date.now()
}

async function main() {

	const url = `https://www.cryptopia.co.nz/api/GetMarkets/USDT`
	try {
		const {statusCode, body} = await Request.getAsync(url, {timeout: config.timeout, json: true})
		// console.log(url, statusCode, body)

		if (statusCode == 200 && body.Success && body.Data ) {
			const ary = config.coins.map(coin => `${coin.toUpperCase()}/USDT`)
			console.log(ary)
			for (var i = body.Data.length - 1; i >= 0; i--) {
				//'TUSD/USDT'
				if (ary.indexOf(body.Data[i].Label) !== -1) {
					console.log(body.Data[i])
					data.Contract = body.Data[i].Label
					data.AskPrice = body.Data[i].AskPrice
					data.AskVolume = body.Data[i].AskVolume
					data.BidPrice = body.Data[i].BidPrice
					data.BidVolume = body.Data[i].BidVolume
					data.LastPrice = body.Data[i].LastPrice
					data.Time = Date.now()
					Cache.hmset(`quotation:tick:exchange:${coin.toUpperCase()}_USDT`, data)
				}
			}
		}
	} catch (error) {
		LOG([market, error.code || error.message || error])
	}

	await Promise.delay(config.timeout * 0.5)
	main()
}

module.exports = main
